
<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.search1"
          size="mini"
          placeholder="输入作者名字查询"
          v-on:input="handleSearch()"
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-select
          size="mini"
          v-model="formInline.search2"
          v-on:change="handleSearch()"
        >
          <el-option label="请选择性别" value=""></el-option>
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          icon="el-icon-circle-plus-outline"
          type="text"
          @click="search = true"
          >查询</el-button
        >
      </el-form-item>

      <el-form-item>
        <el-button
          icon="el-icon-circle-plus-outline"
          type="text"
          @click="dialogAdd = true"
          >添加</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-delete" type="text" @click="handleDeleteList()"
          >删除</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="tableData"
      border
      highlight-current-row
      style="width: 100%"
      @selection-change="handleSelectionDelete"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="作者id">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" prop="authorSex"></el-table-column>
      <el-table-column label="地址" prop="authorAddress"></el-table-column>
      <el-table-column label="名字">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <p>名字: {{ scope.row.authorName }}</p>
            <p>性别: {{ scope.row.authorSex }}</p>
            <p>地址{{ scope.row.authorAddress }}</p>
            <p>手机: {{ scope.row.authorPhone }}</p>
            <p>邮箱: {{ scope.row.authorEmail }}</p>
            <p>qq: {{ scope.row.authorQQ }}</p>
            <p>书籍数量: {{ scope.row.bookCount }}</p>
            <div slot="reference" class="name-wrapper">
              <el-button type="text">{{ scope.row.authorName }}</el-button>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="手机" prop="authorPhone"></el-table-column>
      <el-table-column label="邮箱" prop="authorEmail"></el-table-column>
      <el-table-column label="qq" prop="authorQQ"></el-table-column>
      <el-table-column label="书籍数量" prop="bookCount"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="添加"
      :append-to-body="true"
      :visible.sync="dialogAdd"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
        size="medium"
      >
        <el-form-item label="名称">
          <el-input v-model="ruleForm.authorName"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select size="medium" v-model="ruleForm.authorSex">
            <el-option label="请选择性别" value=""></el-option>
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="ruleForm.authorAddress"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="ruleForm.authorPhone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="ruleForm.authorEmail"></el-input>
        </el-form-item>
        <el-form-item label="qq">
          <el-input v-model="ruleForm.authorQQ"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="emptyUserData()" size="medium">取 消</el-button>
        <el-button @click="addBook('ruleForm')" type="primary" size="medium"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      title="编辑"
      :append-to-body="true"
      :visible.sync="dialogUpdate"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
        class="demo-ruleForm"
        size="medium"
      >
        <el-form-item label="名称">
          <el-input v-model="ruleForm.authorName"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select size="medium" v-model="ruleForm.authorSex">
            <el-option label="请选择性别" value=""></el-option>
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="ruleForm.authorAddress"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="ruleForm.authorPhone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="ruleForm.authorEmail"></el-input>
        </el-form-item>
        <el-form-item label="qq">
          <el-input v-model="ruleForm.authorQQ"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer"
        ><el-button @click="emptyUserData()" size="medium">取 消</el-button>
        <el-button @click="updateBook()" type="primary" size="medium"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <br />

    <el-pagination
      background
      :disabled="disablePage"
      :current-page.sync="currentPage"
      small
      layout="total,prev, pager, next,jumper, sizes"
      :page-size="pageSize"
      :page-sizes="[1, 5, 10, 15]"
      :total="total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>



    <router-link to="/">book</router-link>
  </div>
</template>

<script>
/* eslint-disable */

export default {
  name: "BookHome",
  data() {
    return {
      ruleForm: {
        authorName: null, //作者名称
        authorSex: null, //作者性别
        authorAddress: null, //作者地址
        authorPhone: null, //作者手机
        authorEmail: null, //作者邮箱
        authorQQ: null, //作者qq,
        bookCount: null, //作者书籍数量
      },
      rules: {},
      tableData: [],
      formInline: {
        search1: "",
        search2: "",
      },
      dialogAdd: false,
      dialogUpdate: false,
      pageSize: 5,
      currentPage: 1,
      total: 0,
      disablePage: false,
      multipleSelection: [],
    };
  },

  created() {
    this.handlePageChange();
    this.getRowCount();
  },

  methods: {
    /**
     * 分页
     */
    handlePageChange() {
      let postData = this.qs.stringify({
        pageSize: this.pageSize,
        page: this.currentPage,
        authorName: this.formInline.search1,
        authorSex: this.formInline.search2,
      });
      console.log(this.currentPage);
      this.$axios({
        method: "post",
        url: "/api/queryAuthorListByPage",
        data: postData,
      })
        .then((response) => {
          console.log(response.data);
          this.tableData = response.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },


    /**
      * 每页显示条数
    */

     handleSizeChange(val) {
      this.pageSize = val;
      this.handlePageChange();
      },


    /**
     * 统计书籍总数
     */
    getRowCount() {
      let postData = this.qs.stringify({
        authorName: this.formInline.search1,
        authorSex: this.formInline.search2,
      });
      this.$axios({
        method: "post",
        url: "/api/countAuthor",
        data: postData,
      })
        .then((response) => {
          this.total = response.data;
          console.log(this.total);
        })
        .catch((error) => {
          console.log(error);
        });
    },

    /**
     * 添加书籍
     */
    addBook() {
      if (
        this.ruleForm.authorName == null ||
        this.ruleForm.authorSex == null ||
        this.ruleForm.authorAddress == null ||
        this.ruleForm.authorPhone == null ||
        this.ruleForm.authorEmail == null ||
        this.ruleForm.authorQQ == null
      ) {
        this.$alert("作者信息不完整请检查", "温馨提示", {
          confirmButtonText: "确定",
        });
        return;
      }
      let postData = this.qs.stringify({

        authorName: this.ruleForm.authorName, //作者名称
        authorSex: this.ruleForm.authorSex, //作者性别
        authorAddress: this.ruleForm.authorAddress, //作者地址
        authorPhone: this.ruleForm.authorPhone, //作者手机
        authorEmail: this.ruleForm.authorEmail, //作者邮箱
        authorQQ: this.ruleForm.authorQQ, //作者qq
      });
      this.$axios({
        method: "post",
        url: "/api/addAuthor",
        data: postData,
      })
        .then((response) => {
          if(response.data===0){
             this.$message({
              type: "error",
              message: "作者已经存在!",
          })
            return;
          }
          this.handlePageChange();
          this.getRowCount();
          this.$message({
            type: "success",
            message: "已添加!",
          });
          this.emptyUserData();
        })
        .catch((error) => {
          console.log(error);
        });
    },
    search() {
      this.handlePageChange();
      this.getRowCount();
    },

    handleSearch() {
      this.handlePageChange();
      this.getRowCount();
    },

    handleEdit(index, row) {
      this.dialogUpdate = true;
      this.ruleForm = Object.assign({}, row, index);
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.emptyUserData();
        })
        .catch((_) => {});
    },

    /**
     * 清空绑定数据
     */
    emptyUserData() {
      this.dialogAdd = false;
      this.dialogUpdate = false;
      this.ruleForm = {
        authorName: null, //作者名称
        authorSex: null, //作者性别
        authorAddress: null, //作者地址
        authorPhone: null, //作者手机
        authorEmail: null, //作者邮箱
        authorQQ: null, //作者qq
      };
    },

    /**
     * 根据 id 删除作者
     * @param index
     * @param row
     */
    handleDelete(index, row) {
      this.$confirm("删除操作, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let postData = this.qs.stringify({
            id: row.id,
          });
          this.$axios({
            method: "post",
            url: "/api/deleteAuthor",
            data: postData,
          })
            .then((response) => {
              if(response.data === 0) {
                this.$message({
                  type: "error",
                  message: "作者有书籍存在，不能删除!",
                });
                return;
              }
              this.getRowCount();
              this.handlePageChange();

              this.$message({
                type: "success",
                message: "删除成功!",
              });
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    handleSelectionDelete(val) {
      this.multipleSelection = val;
    },

    /**
     * 根据 Id 批量删除书籍
     */
    handleDeleteList() {
      let Ids = "";
      this.multipleSelection.forEach((item) => {
        Ids += item.id + ",";
      });

      this.$confirm("删除操作, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((response) => {
          let postData = this.qs.stringify({
            idList: Ids,
          });
          console.log(postData);
          this.$axios({
            method: "post",
            url: "/api/deleteAuthorByIds",
            data: postData,
          })
            .then((response) => {
              if(response.data === 0) {
                this.$message({
                  type: "error",
                  message: "作者有书籍存在，不能删除!",
                });
                return;
              }
              this.getRowCount();
              this.handlePageChange();

              this.$message({
                type: "success",
                message: "删除成功!",
              });
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    updateBook() {
      if (
        this.ruleForm.authorName == null ||
        this.ruleForm.authorSex == null ||
        this.ruleForm.authorAddress == null ||
        this.ruleForm.authorPhone == null ||
        this.ruleForm.authorEmail == null ||
        this.ruleForm.authorQQ == null
      ) {
        this.$alert("作者信息不完整请检查", "温馨提示", {
          confirmButtonText: "确定",
        });
        return;
      }
      let postData = this.qs.stringify({
        id: this.ruleForm.id,
        authorName: this.ruleForm.authorName, //作者名称
        authorSex: this.ruleForm.authorSex, //作者性别
        authorAddress: this.ruleForm.authorAddress, //作者地址
        authorPhone: this.ruleForm.authorPhone, //作者手机
        authorEmail: this.ruleForm.authorEmail, //作者邮箱
        authorQQ: this.ruleForm.authorQQ, //作者qq
      });
      this.$axios({
        method: "post",
        url: "/api/updateAuthor",
        data: postData,
      })
        .then((response) => {
           if(response.data===0){
             this.$message({
              type: "error",
              message: "作者已经存在!",
          })
            return;
          }
          this.handlePageChange();
          this.getRowCount();
          this.$message({
            type: "success",
            message: "已编辑!",
          });
          this.emptyUserData();
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
/* eslint-disable */
</script>
<style scoped>
</style>
